package com.tgb.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.tgb.entity.UserInfo;

@Repository
public class UserInfoDaoImpl implements UserInfoDao {
	@Resource
	private SessionFactory sessionFactory;

	/*public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}*/

	@Override
	public UserInfo getUserInfo(int id) {
		String hql = "from UserInfo ui where ui.id=?";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setInteger("id", id);
		return (UserInfo)query.uniqueResult();
	}

	@Override
	public void addUserInfo(UserInfo userInfo) {
		sessionFactory.getCurrentSession().flush();
		sessionFactory.getCurrentSession().save(userInfo);
	}

	@Override
	public List<UserInfo> getAllUserInfos() {
		
		String hq2 = "from UserInfo";
		Query query = sessionFactory.getCurrentSession().createQuery(hq2);
		
		return query.list();

	}

	@Override
	public boolean delUserInfo(int id) {
		

		String hql = "delete UserInfo u where u.id = ?";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setInteger(0, id);
		
		return (query.executeUpdate() > 0);
		
	}

	@Override
	public boolean updateUserInfo(UserInfo userInfo) {
		
		String hql = "update UserInfo u set u.infoString = ? where u.id = ?";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, userInfo.getInfo());
		
		query.setInteger(1, userInfo.getId());
		 
		return (query.executeUpdate() > 0);
		
	}
	

}